﻿using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;

namespace System {
    /// <summary>
    /// 一个默认使用文件进行记录的日志器。
    /// </summary>
    public class FileLogger:LoggerBase {
        public override void LogInfo(string message) {
            AppendAllText(message,LogType.Info.ToString());
        }
        public override void LogWarn(string message) {
            AppendAllText(message,LogType.Warn.ToString());
        }
        public override void LogDebug(string message) {
            AppendAllText(message,LogType.Debug.ToString());
        }
        public override void LogTrace(string message) {
            AppendAllText(message,LogType.Trace.ToString());
        }
        public override void LogSql(string message) {
            AppendAllText(message,LogType.Sql.ToString());
        }
        public override void LogError(string title,Exception e) {
            AppendAllText(title+e.Message,LogType.Error.ToString());
        }
        public override void LogError(string message) {
            AppendAllText(message,LogType.Error.ToString());
        }
        public override void LogFile(string message,string filename) {
            AppendAllText(message+Environment.NewLine,LogType.Error.ToString());
        }
        private void AppendAllText(string contents,string filename) {

            object result=null;
            try {
                result=Enum.Parse(typeof(LogType),filename);
            } catch {

            }
            if (result == null) result = filename;

            try {
                if(string.IsNullOrWhiteSpace(contents)) return;
                var trace = new StackTrace();

                StringBuilder builder=new StringBuilder();
                builder.AppendLine("AppDomain:" + AppDomain.CurrentDomain.FriendlyName);
                builder.AppendLine("Message:"+contents);
                builder.AppendLine("StackTrace:" + trace.ToString());
                builder.AppendLine("========================================================================================");
                string path = System.IO.Directory.GetCurrentDirectory()+ "//Logs" ;
                string logfile = path+"/"+result.ToString().ToLower()+DateTime.Now.ToString("yyyyMMdd")+".log";
                if(!Directory.Exists(path)) {
                    Directory.CreateDirectory(path);
                }
                File.AppendAllText(logfile, builder.ToString());
                if(result.ToString()==LogType.Error.ToString()) {
                    try {
                        Async.AsyncRun(delegate {
                            string tit = contents.Length>10 ? Utils.GetFirstString(contents,10) : "";
                            SendEmail(result.ToString().ToLower()+tit,contents);
                        });
                    } catch {

                    }
                }
            } catch {

            }
        }
        private void SendEmail(string Title,string Content) {
            bool Result = EmailHepler.SendEmail("1640601039@qq.com",Title, Content);
        }
    }
}